// 封装fetch
import qs from 'qs'

class Ajax {
    get(url) {
        return new Promise((resolve, reject) => {
            fetch(url).then(res => res.json()).then(data => resolve(data)).catch(err => reject(err))
        })
    }

    // post方式
    post(url, data) {
        return new Promise((resolve, reject) => {
            fetch(url, {
                method: 'POST',
                mode: 'cors',
                credentials: 'include',
                headers: {
                    'Content-type': 'application/json',
                    'Access-Control-Allow-Origin': '*'
                },
                body: JSON.stringify(data)
            }).then(res => res.json()
            )
                .then(data => resolve(data))
                .catch(err => reject(err))

        })
    }


    //put 修改
    put(url, data) {
        return new Promise((resolve, reject) => {
            fetch(url, {
                method: 'PUT',
                mode: 'cors',
                credentials: 'include',
                headers: {
                    'Content-type': 'application/json',
                    'Access-Control-Allow-Origin': '*'
                },
                body: JSON.stringify(data)
            })
                .then(res => res.json())
                .then(data => resolve(data))
                .catch(err => reject(err))

        })
    }

    //delete
    delete(url, data) {
        return new Promise((resolve, reject) => {
            fetch(url, {
                method: 'DELETE',
                mode: 'cors',
                credentials: 'include',
                headers: {
                    'Content-type': 'application/json',
                    'Access-Control-Allow-Origin': '*'
                },
                body: JSON.stringify(data)
            })
                .then(res => res.json())
                .then(data => resolve('数据删除成功!'))
                .catch(err => reject(err))
        })
    }

    // 'Content-Type':'application/x-www-form-urlencoded'
    formUrlencoded(url, data){
        let body = qs.stringify(data)
        return new Promise((resolve, reject) => {
            fetch(
                url,
                {
                    method:'POST',
                    mode: 'cors',
                    credentials: 'include',
                    headers:{
                        'Content-Type':'application/x-www-form-urlencoded',
                        'Access-Control-Allow-Origin': '*'
                    },
                    body
                }
            ).then(res => res.json()).then(data => resolve(data)).catch(err => reject(err))
        })
    }


    postFromData(url, data){
        return new Promise((resolve, reject) => {
            fetch(url, {
                method: 'POST',
                body: data
            })
                .then(res => res.json())
                .then(data => resolve(data))
                .catch(err => reject(err))

        })
    }
}
export default new Ajax();//ES6导出
